//
//  CTThirtPartyRegisterView.m
//  ConstellationTalk_0.1
//
//  Created by riley on 15/4/20.
//  Copyright (c) 2015年 hanwei. All rights reserved.
//

#import "CTThirtPartyRegisterView.h"
#import "CTDebugManager.h"
#import "CTAppLanguage.h"
#import "CTHeader.h"
#import "UIViewAdditions.h"
#import "CTLoginView_Def.h"

@interface CTThirtPartyRegisterView()<UITextFieldDelegate>
{
    UIImageView *headButton;
    UITextField *userTextField;
    UIView *topView;
    UIImageView *userNameImageView;
    UIImageView *imageView;
    
}
@end

@implementation CTThirtPartyRegisterView
@synthesize delegate;
-(void)dealloc
{
    headButton = nil;
    userTextField = nil;
    topView = nil;
    delegate = nil;
    userNameImageView = nil;
    imageView = nil;
    [CTDebugManager showDebugLogInfo:@"dealloc : CTThirtPartyRegisterView Is Removed!!!"];
}

- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = CT_HEADER_WHITE_COLOR;
        self.frame = CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT);
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapACtion:)];
        [self addGestureRecognizer:tap];
        
        [self createThirtPartyTitleView];
        [self createThirtPartyRegisterView];
        
    }
    
    return self;
}
//标题View
- (void)createThirtPartyTitleView
{
    topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CT_HEADER_SCREEN_WIDTH, 64)];
    topView.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:topView];
    
    //标题
    UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 20, CT_HEADER_SCREEN_WIDTH, 44)];
    titleLabel.font = [UIFont fontWithName:nil size:19];
    titleLabel.text = [CTAppLanguage localizedStringForKey:@"thirtPartyRegisterView_title_text" withValue:nil withTable:@"Language"];
    titleLabel.textColor = CT_HEADER_GREEN_COLOR;
    
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [topView addSubview:titleLabel];
    
    //返回按钮
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 44, 44);
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE] forState:UIControlStateNormal];
    backButton.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [backButton setImage:[UIImage imageNamed:CT_HEADER_BACK_IMAGE_CLICK] forState:UIControlStateHighlighted];
    [topView addSubview:backButton];
    
    //完成
    UIButton *finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    finishButton.frame = CGRectMake(CT_HEADER_SCREEN_WIDTH-44,20 , 44, 44);
    [finishButton addTarget:self action:@selector(finishButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [finishButton setTitle:[CTAppLanguage localizedStringForKey:@"thirtPartyRegisterView_Finish_Btn_text" withValue:nil withTable:@"Language"] forState:UIControlStateNormal];
    [finishButton setTitleColor:CT_HEADER_GREEN_COLOR forState:UIControlStateNormal];
    [finishButton setTitleColor:LOGINVIEW_USER_REGISTER_TEXT_HEIGHT_COLOR forState:UIControlStateHighlighted];
    [topView addSubview:finishButton];
    
    
    

}
//View
- (void)createThirtPartyRegisterView
{
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, topView.bottom, CT_HEADER_SCREEN_WIDTH, CT_HEADER_SCREEN_HEIGHT-topView.height)];
    view.backgroundColor = CT_HEADER_WHITE_COLOR;
    [self addSubview:view];
    //头像
    userNameImageView = [[UIImageView alloc]initWithFrame:CGRectMake((view.width-80)/2, 65, 80, 80)];
    userNameImageView.layer.cornerRadius = 40;
    userNameImageView.layer.masksToBounds = YES;
    userNameImageView.userInteractionEnabled = YES;
    userNameImageView.image = [UIImage imageNamed:@"head"];
    [view addSubview:userNameImageView];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(headButtonAction:)];
    [userNameImageView addGestureRecognizer:tap];
    
    
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake((view.width-80)/2, 65, 80, 80)];
    imageView.image = [UIImage imageNamed:@"registrationinformation_mengban"];
    [view addSubview:imageView];
    
    headButton = [[UIImageView alloc]init];
    headButton.frame = CGRectMake(imageView.origin.x+23, 94, 34, 22.5);
    headButton.image = [UIImage imageNamed:@"registrationinformation_mengban"];
    [view addSubview:headButton];
    
    //用户名
    UIImageView *userImageView = [[UIImageView alloc]initWithFrame:CGRectMake((CT_HEADER_SCREEN_WIDTH-280)/2, userNameImageView.bottom+22, 280, 50)];
    userImageView.image = [UIImage imageNamed:@"resetting_password_shurukuang"];
    [view addSubview:userImageView];
    
    NSString *userLabelText = [CTAppLanguage localizedStringForKey:@"registerView_userName_text" withValue:nil withTable:@"Language"];
    UILabel *userLabel = [[UILabel alloc]initWithFrame:CGRectMake(userImageView.origin.x+10, userImageView.origin.y, userLabelText.length*15, 50)];
    userLabel.font = [UIFont fontWithName:nil size:15];
    userLabel.text = userLabelText;
    userLabel.textColor = LOGINVIEW_USER_HEAD_TEXTFIELD_COLOR;
    userLabel.textAlignment = NSTextAlignmentLeft;
    [userNameImageView addSubview:userLabel];
    [view addSubview:userLabel];
    
    userTextField = [[UITextField alloc]initWithFrame:CGRectMake(userLabel.right, userLabel.origin.y, userImageView.width-userLabel.width, 50)];
    userTextField.placeholder = [CTAppLanguage localizedStringForKey:@"registerView_userTextField_text" withValue:nil withTable:@"Language"];
    userTextField.font = [UIFont fontWithName:nil size:15];
    userTextField.textColor = LOGINVIEW_USER_TEXTFIELD_COLOR;
    userTextField.textAlignment = NSTextAlignmentLeft;
    userTextField.delegate = self;
    [view addSubview:userTextField];
}
#pragma mark ButtonAction
//返回
- (void)backButtonAction:(UIButton *) sender
{
    if ([self.delegate respondsToSelector:@selector(thirtPartyRegisterViewBackButton:)])
    {
        [self.delegate thirtPartyRegisterViewBackButton:self];
    }
}
//完成
- (void)finishButtonAction:(UIButton *) sender
{
    [userTextField resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(thirtPartyRegisterViewFinishButton:)])
    {
        [self.delegate thirtPartyRegisterViewFinishButton:self];
    }
}

//头像
- (void)headButtonAction:(UITapGestureRecognizer *) sender
{
    if ([self.delegate respondsToSelector:@selector(thirtPartyRegisterViewHeadButton:)])
    {
        [self.delegate thirtPartyRegisterViewHeadButton:self];
    }
}
-(void)setImage:(UIImage *)image
{
    [headButton removeFromSuperview];
    [imageView removeFromSuperview];
    userNameImageView.image = image;
}

#pragma markd TextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    
}// became first responder

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [userTextField resignFirstResponder];
    if ([self.delegate respondsToSelector:@selector(thirtPartyRegisterViewUserTextFieldText:registerView:)])
    {
        [self.delegate thirtPartyRegisterViewUserTextFieldText:textField.text registerView:self];
    }
}
//收回键盘
- (void)tapACtion:(UITapGestureRecognizer *)tap
{
    [userTextField resignFirstResponder];
}
@end
